package ch.zhaw.swp1.view;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import ch.zhaw.swp1.model.message.Message;
import ch.zhaw.swp1.model.status.Status;

/**
 * Helper class for dialogs
 * @author Daniel Tiefenauer
 */
public class StatusOptionPane extends JOptionPane {
	
	/**
	 * Default version
	 */
	private static final long serialVersionUID = 1L;

	private static final String sendDatePattern = "dd.MM.yyyy";
	private static final String sendTimePattern = "HH:mm";

	private static final ImageIcon iconOK = new ImageIcon("images/icon_ok.png");
	private static final ImageIcon iconError = new ImageIcon("images/icon_error.png");


	/************************************************************************************/
	// Methods
	/*----------------------------------------------------------------------------------*/
	/**
	 * Show a dialog concerning the status of a message
	 * @param message the message involved
	 */
	public static void showMessageSentDialog(Message message){
		// Message has no send date ==> has been sent immediately
		if (message.getSendDate() == null){
			showMessageHasBeenSentDialog();
		}
		// Message has a send date ==> will be sent later
		else{
			showMessageWillBeSentDialog(message);
		}
	}
	
	/**
	 * Shows a dialog that a message with no delay has been sent 
	 */
	private static void showMessageHasBeenSentDialog(){
		JOptionPane.showMessageDialog(getRootFrame(), "Nachricht wurde gesendet", "Nachricht OK", INFORMATION_MESSAGE, iconOK);
	}
	
	/**
	 * Shows a dialog that a delayed message will be sent
	 * @param message
	 */
	public static void showMessageWillBeSentDialog(Message message){
		// Create send date/time string
		String sendDateString = "";
		String sendTimeString = "";
		Date sendDate = new Date(message.getSendTime());
		SimpleDateFormat df = new SimpleDateFormat(sendDatePattern);
		sendDateString = df.format(sendDate);
		df = new SimpleDateFormat(sendTimePattern);
		sendTimeString = df.format(sendDate);

		String confirmationMessage = "Nachricht wird gesendet am " + sendDateString + " um " + sendTimeString;
		JOptionPane.showMessageDialog(getRootFrame(), confirmationMessage, "Nachricht OK", INFORMATION_MESSAGE);		
	}
	
	/**
	 * Shows a dialog that a message contains errors
	 * @param message
	 */
	public static void showMessageStatusDialog(Message message){
		JPanel contentPane = new JPanel();
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		List<Status> statusList = message.getStatusList();
		Iterator<Status> it = statusList.iterator();
		while(it.hasNext()){
			contentPane.add(new JLabel(it.next().getStatusMessage()));
		}
		JOptionPane.showMessageDialog(getRootFrame(), contentPane, "Fehler bei der Validierung", ERROR_MESSAGE, iconError);
	}

}
